<?php
class Videos_model extends Model{
	
	var $_max_video 	= 3;
	var $_datagrid_name = 'dt1';	
	
    function Videos_model(){
        parent::Model();       
    }
	function getdatasource(){ 
    	$page = $this->input->post($this->_datagrid_name.'_hidden_page')?$this->input->post($this->_datagrid_name.'_hidden_page'):1;    	
    	$limit = $this->config->item('record_limit');   	
    	$count = $this->db->count_all('video');    	
    	$pages = intval($count/$limit) + ($count%$limit?1:0);
    	$page = $page>$pages?1:$page;
    	$this->db->limit($limit,($page-1)*$limit);
    	$this->db->select('id,categoryid,image,filename,sortorder');    	
    	$this->db->order_by('categoryid,sortorder,id');
    	$query = $this->db->get('video');
    	$dts = $query->result_rowlist();
    	for($i=0;$i<$query->num_rows;$i++){   
			$dts[$i][1] = get_content('productcategory',$dts[$i][1])->categoryname;
    		$dts[$i][2] = base_url().$this->config->item('video_img_path').$dts[$i][2];			
    	}
    	$datagrid->id = $this->_datagrid_name;
    	$datagrid->page = $page;
    	$datagrid->pages = $pages;
    	$datagrid->datasource = $dts;    	
    	return $datagrid;    	
    }    
    function getcategorylist(){
		$this->db->where('superid != 0');
		$this->db->order_by('superid,sortorder,id');
    	$res = $this->db->get('productcategory');
		$category = array();
		$cat = $res->result();
		for($i=0;$i<$res->num_rows();$i++){			
			$category[$i]->id = $cat[$i]->id;
			$category[$i]->name = get_content('productcategory',$cat[$i]->id)->categoryname;
		}
		return $category;
    }
	function delete(){
    	if($this->input->post($this->_datagrid_name.'_hidden_value')){
    		$id = $this->input->post($this->_datagrid_name.'_hidden_value');
    		$this->_deleteimage($id);
    		$this->db->delete('video',array('id'=>$id));
    	}
    	else if($this->input->post($this->_datagrid_name.'_chk_select')){
    		$id = $this->input->post($this->_datagrid_name.'_chk_select');
    		for($i=0;$i<count($id);$i++){
    			$this->_deleteimage($id[$i]);
	    		$this->db->delete('video',array('id'=>$id[$i]));
    		}
    	}
    }
	function getnewvideo(){
		$res->categoryid = $this->input->post('cbo_category');		
		$res->filename = $this->input->post('txt_filename');
		$res->alertmsg = empty($res->categoryid)?"":sprintf(lang('videos_msg_maxvideo'),$this->_max_video);
		return $res;
	}
    function addnew(){
    	if(FALSE!==$this->input->post('hidden_status')){
			$categoryid = $this->input->post('cbo_category');
			$res = $this->db->get_where('video',array('categoryid'=>$categoryid));
			if($res->num_rows() < $this->_max_video){
				$image_name = '';
	    		$config['upload_path'] = $this->config->item('video_img_path');
				$config['allowed_types'] = $this->config->item('upload_allowed_types');
				$this->load->library('upload', $config);
	    		if($this->upload->do_upload('txt_image')){
	    			$upload_data = $this->upload->data();
	    			$image_name = $upload_data['file_name'];
	    		}    		    		
	    		$this->db->insert('video',array('categoryid'=>$categoryid,'image'=>$image_name,'filename'=>$this->input->post('txt_filename'),'sortorder'=>0));
	    		redirect('videos','location');
			}			   		
    	}
    }
    function update(){
    	if(FALSE!==$this->input->post('hidden_status')){    		
    		$image_name = '';
    		$config['upload_path'] = $this->config->item('video_img_path');
			$config['allowed_types'] = $this->config->item('upload_allowed_types');
			$this->load->library('upload', $config);
    		if($this->upload->do_upload('txt_image')){
    			$upload_data = $this->upload->data();
    			$image_name = $upload_data['file_name'];
    			$this->_deleteimage($this->input->post('hidden_id'));
    		}
    		if(!empty($image_name)){
    			$this->db->set('image',$image_name);
    		}    		
    		$this->db->where('id',$this->input->post('hidden_id'));
    		$this->db->update('video',array('categoryid'=>$this->input->post('cbo_category'),'filename'=>$this->input->post('txt_filename')));
    		redirect('videos','location');
    	}
    }
    function getdetail($id){
    	$query = $this->db->get_where('video',array('id'=>$id));
    	if($query->num_rows){
	    	return $query->row();
    	}
    	else return FALSE;
    }
    function save(){
    	if(FALSE!==$this->input->post('hidden_status')){
    		$id = $this->input->post($this->_datagrid_name.'_hidden_id');
    		$sortorder = $this->input->post($this->_datagrid_name.'_column_4');
    		for($i=0;$i<count($id);$i++){
    			$this->db->where('id',$id[$i]);
    			$this->db->update('video',array('sortorder'=>$sortorder[$i]));
    		}    		
    	}
    }
    function _deleteimage($id){
    	$res = $this->db->get_where('video',array('id'=>$id));
    	$image = $res->row()->image;
    	if(file_exists($this->config->item('video_img_path').$image)){
    		@unlink($this->config->item('video_img_path').$image);
    	}
    }
}

/* End of file */